﻿using BLL;
using Model;
using System;
using Thksoft.Modal.Page;

namespace Thksoft.Modal
{
    public partial class EditRoleGroup : FormPage
    {
        private Sys_RoleBLL roleBLL = new Sys_RoleBLL();
        private Sys_RoleGroupBLL roleGroupBLL = new Sys_RoleGroupBLL();

        protected override void BindPage()
        {
            if (FORM_IFEDIT)
            {
                var entity = roleGroupBLL.Find(Convert.ToDecimal(FORM_KEY));
                FORM_ROLE_NAME.Text = entity.RGRO_NAME;
                FORM_ROLE_STATE.SelectedValue = entity.RGRO_STATE;
                BinRole(entity.ROLE_ID);
            }
        }

        private void BinRole(string idStr)
        {
            var text = "";
            if (idStr != "")
            {
                text = roleBLL.GetRoleNames(idStr);
            }
            FORM_ROLE_IDS.Value = idStr;
            FORM_ROLE_STRING.Text = text;
        }

        protected override void SaveClick(object sender, EventArgs e)
        {
            if (FORM_ROLE_NAME.Text == "")
            {
                MessageBox.Show(this, "请选择角色组名称");
                return;
            }
            if (FORM_ROLE_STRING.Text == "" || FORM_ROLE_IDS.Value == "")
            {
                MessageBox.Show(this, "请选择系统角色");
                return;
            }

            Sys_RoleGroup entity;
            if (FORM_IFEDIT)
            {
                entity = roleGroupBLL.Find(Convert.ToDecimal(FORM_KEY));

                #region 修改

                entity.RGRO_NAME = FORM_ROLE_NAME.Text.Trim();
                entity.RGRO_STATE = FORM_ROLE_STATE.SelectedValue;
                entity.ROLE_ID = FORM_ROLE_IDS.Value;

                if (roleGroupBLL.UPDATE(entity, CurrentUserName))
                {
                    MessageBox.Show(this, "修改成功");
                }
                else
                {
                    MessageBox.Show(this, "修改失败");
                }

                #endregion 修改
            }
            else
            {
                #region 新增

                entity = new Sys_RoleGroup
                {
                    RGRO_NAME = FORM_ROLE_NAME.Text.Trim(),
                    ROLE_ID = FORM_ROLE_IDS.Value,
                    RGRO_STATE = FORM_ROLE_STATE.SelectedValue
                };
                if (roleGroupBLL.INSERT(entity, CurrentUserName))
                {
                    MessageBox.ShowAndRedirect(this, "新增成功", "/Modal/EditRoleGroup.aspx?IF_EDIT=FALSE");//TRUE&ID=" + entity.RGRO_ID.ToString());
                }
                else
                {
                    MessageBox.Show(this, "新增失败");
                }

                #endregion 新增
            }
        }

        protected override void UpdateTitle()
        {
            if (FORM_IFEDIT)
            {
                MessageBox.ResponseScript(this, "FORM_UPDATE_TITLE(\"修改角色组\");");
            }
            else
            {
                MessageBox.ResponseScript(this, "FORM_UPDATE_TITLE(\"添加角色组\");");
            }
        }
    }
}